package com.nxcy.com.nxcy.servlet;


import com.google.gson.Gson;
import com.mongodb.client.*;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.bson.conversions.Bson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet(urlPatterns = {"/student/save.do"})
public class SaveStudentServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String url = "mongodb://g08:abc%408@mongo.zhledu.cn:27017/g08";
        MongoClient client = MongoClients.create(url);
        MongoIterable<String> names = client.listDatabaseNames();
        MongoDatabase g08 = client.getDatabase("g08");
        MongoCollection<Document> collection = g08.getCollection("student");
        String id = request.getParameter("_id");
        Integer age = Integer.parseInt(request.getParameter("age"));
        String name = request.getParameter("name");
        String sno = request.getParameter("sno");
        String sex = request.getParameter("sex");
        String birthDay = request.getParameter("birthDay");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String major = request.getParameter("major");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String idCard = request.getParameter("idCard");
        String email = request.getParameter("email");

        Document result = new Document();
        Document student = new Document();
        student.append("name", name)
                .append("sex", sex)
                .append("sno", sno)
                .append("birthDay", birthDay)
                .append("phone", phone)
                .append("username", username)
                .append("password", password)
                .append("major", major)
                .append("idCard", idCard)
                .append("address", address)
                .append("email", email)
                .append("age", age);

        Document query = new Document();
        if ("0".equals(id)) {
            List<Bson> queryList = new LinkedList<>();
            Document group = new Document().append("$group", new Document("_id", 1).append("max", new Document("$max", "$_id")));
            queryList.add(group);
            AggregateIterable<Document> list = collection.aggregate(queryList);
            for (Document doc : list) {
                query.append("_id", doc.getInteger("_id") + 1);
                break;
            }
        } else {
            query.append("_id", Integer.parseInt(id));
        }

        Document set = new Document();
        set.append("$set", student);
        UpdateOptions options = new UpdateOptions();
        options.upsert(true);
        final UpdateResult updateResult = collection.updateMany(query, set, options);
        if (updateResult.getModifiedCount() > 0) {
            result.append("code", 0);
        } else {
            result.append("code", -1);
        }
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        Gson gson = new Gson();
        response.getWriter().write(gson.toJson(result));
    }
}
